package com.rongshu.sendmessage.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.Nullable;
import com.alibaba.fastjson.JSON;
import com.rongshu.sendmessage.util.ToastUtils;
import com.rongshu.sendmessage.util.event.ShowGuidEvent;
import com.rongshu.sendmessage.util.wx.Constant_Third_ID;
import com.rongshu.sendmessage.util.wx.WXBaseRespEntity;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    /**
     * 微信登录相关
     */
    private IWXAPI api;
    private String code;
    private WXEntryActivity mContext;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //通过WXAPIFactory工厂获取IWXApI的示例
        api = WXAPIFactory.createWXAPI(this, Constant_Third_ID.weixin_app_id, true);
        //将应用的appid注册到微信
        api.registerApp(Constant_Third_ID.weixin_app_id);
        mContext=this;
        //注意：
        //第三方开发者如果使用透明界面来实现WXEntryActivity，需要判断handleIntent的返回值，如果返回值为false，则说明入参不合法未被SDK处理，应finish当前透明界面，避免外部通过传递非法参数的Intent导致停留在透明界面，引起用户的疑惑
        try {
            boolean result = api.handleIntent(getIntent(), this);
            if (!result) {
                Log.d("Dong", "参数不合法，未被SDK处理，退出");
                finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        api.handleIntent(data, this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
        finish();
    }

    @Override
    public void onReq(BaseReq baseReq) {
        Log.d("baseReq:", JSON.toJSONString(baseReq));
    }

    @Override
    public void onResp(BaseResp baseResp) {
        Log.d("baseResp", JSON.toJSONString(baseResp));
        Log.d("baseResp", baseResp.errStr + "," + baseResp.openId + "," + baseResp.transaction + "," + baseResp.errCode);
        String result = "";
        WXBaseRespEntity entity = JSON.parseObject(JSON.toJSONString(baseResp), WXBaseRespEntity.class);
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                result = "分享成功";
                code = entity.getCode();
                showMsg(1, result);
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                result = "分享取消";
                showMsg(2, result);
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                result = "分享被拒绝";
                showMsg(3, result);
                break;
            default:
                result = "分享返回";
                showMsg(0, result);
                break;
        }

    }

    private void showMsg(int i, String result) {
        if(i==1){
            ToastUtils.show(WXEntryActivity.this,result);
            ShowGuidEvent lShowGuidTwoEvent = new ShowGuidEvent();
            lShowGuidTwoEvent.setIndex(1000);
            lShowGuidTwoEvent.setCode(code);
            EventBus.getDefault().post(lShowGuidTwoEvent);
            finish();
            return;
        }
        ToastUtils.show(WXEntryActivity.this,result);
        finish();
    }
}
